package com.chenyongjing.design.pattern.strategy.promotion;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 策略工厂，解决if和else的问题
 */
public class PromotionStrategyFactory {
    private static Map<String,IPromotionStrategy> PROMOTIONS = new HashMap<String,IPromotionStrategy>();
    static {
        PROMOTIONS.put(PromotionKey.COUPON,new CouponStrategy());
        PROMOTIONS.put(PromotionKey.GROUPBUY,new GroupbuyStrategy());
        PROMOTIONS.put(PromotionKey.CASHBACK,new CashbackStrategy());
    }

    // 设置默认策略
    private static final IPromotionStrategy EMPTY = new EmptyStrategy();

    private PromotionStrategyFactory(){

    }

    // 策略工厂逻辑
    public static  IPromotionStrategy getPromotionStrategy(String promotionKey){
        IPromotionStrategy strategy = PROMOTIONS.get(promotionKey);
        return strategy == null ? EMPTY : strategy;
    }

    // 处理if...else...的问题
    private interface PromotionKey{
        String COUPON ="COUPON";
        String GROUPBUY ="GROUPBUY";
        String CASHBACK ="CASHBACK";
    }

    public static Set<String> getPromotionKeys(){
        return PROMOTIONS.keySet();
    }
}
